library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.1 ──
✔ ggplot2 3.3.6 ✔ purrr 0.3.4
✔ tibble 3.1.7 ✔ dplyr 1.0.9
✔ tidyr 1.2.0 ✔ stringr 1.4.0
✔ readr 2.1.2 ✔ forcats 0.5.1
── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
international <- read_csv("clean_data/international.csv")
Warning: One or more parsing issues, see `problems()` for details
Rows: 32049 Columns: 9
── Column specification ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (5): country, purpose, mode, duration, age
dbl (4): year, visits_thousands, nights_thousands, expenditure_millions
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
international
usa <- international %>%
filter(country == "USA") %>%
mutate(year = as.character(year)) %>%
drop_na()
usa
usa %>%
ggplot(aes(x = visits_thousands,
y = expenditure_millions)) +
geom_point()

usa_scale <- usa %>%
mutate(across(where(is_numeric), scale)) %>%
select(visits_thousands, expenditure_millions)
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
Warning: Problem while computing `..1 = across(where(is_numeric), scale)`.
ℹ Deprecated
head(usa_scale, 10)
usa_scale %>%
ggplot(aes(x = visits_thousands,
y = expenditure_millions)) +
geom_point()

library(broom)
max_k <- 20
k_clusters <- tibble(k = 1:max_k) %>%
mutate(
kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25)),
tidied = map(kclust, tidy),
glanced = map(kclust, glance),
augemnted = map(kclust, augment, usa)
)
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
Warning: Problem while computing `kclust = map(k, ~kmeans(usa_scale, .x, nstart = 25))`.
ℹ did not converge in 10 iterations
k_clusters
augmented <- k_clusters %>%
unnest(augemnted)
augmented
augmented %>%
filter(k == 3) %>%
count(.cluster)
clusterings <- k_clusters %>%
unnest(glanced)
clusterings
ggplot(clusterings, aes(x=k, y=tot.withinss)) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = seq(1, 20, by = 1))

library(factoextra)
Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_nbclust(usa_scale,
kmeans,
method = "wss",
nstart = 25)

fviz_nbclust(usa_scale,
kmeans,
method = "silhouette",
nstart = 25)
Warning: did not converge in 10 iterations

fviz_nbclust(usa_scale,
kmeans,
method = "gap_stat",
nstart = 25,
k.max = 10)
Clustering k = 1,2,..., K.max (= 10): ..
Warning: did not converge in 10 iterations
done
Bootstrapping, b = 1,2,..., B (= 100) [one "." per sample]:
................
Warning: did not converge in 10 iterations
...............
Warning: Quick-TRANSfer stage steps exceeded maximum (= 180450)
...............
Warning: Quick-TRANSfer stage steps exceeded maximum (= 180450)
.... 50
augmented <- augmented %>%
filter(k == 3) %>%
mutate(cluster_group = case_when(
.cluster == 3 ~ "smaller spenders",
.cluster == 1 ~ "big spenders",
.cluster == 2 ~ "medium spenders"
)) %>%
select(-.cluster)
augmented %>%
filter(k == 3) %>%
ggplot(aes(x = visits_thousands,
y = expenditure_millions))+
geom_point(aes(colour = cluster_group))+
facet_wrap(~ k) +
labs(x = "\nVisits thousands",
y = "Expenditure millions",
title = "Expenditure vs Visits for Tourists from the USA",
subtitle = "Years: 2002 - 2019\n") +
theme_minimal()

augmented %>%
filter(k == 3) %>%
filter(visits_thousands > 30)
augmented %>%
filter(k == 3) %>%
count(cluster_group) %>%
ggplot(aes(x = cluster_group,
y = n)) +
geom_col()

augmented %>%
filter(k == 3) %>%
group_by(cluster_group) %>%
summarise(total_expenditure = sum(expenditure_millions)) %>%
ggplot(aes(x = cluster_group,
y = total_expenditure)) +
geom_col()

augmented %>%
filter(k == 3) %>%
group_by(cluster_group) %>%
summarise(mean_expenditure = mean(expenditure_millions)) %>%
ggplot(aes(x = cluster_group,
y = mean_expenditure)) +
geom_col()

augmented %>%
filter(k == 3) %>%
group_by(cluster_group) %>%
summarise(total_visits = sum(visits_thousands)) %>%
ggplot(aes(x = cluster_group,
y = total_visits)) +
geom_col()

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, age) %>%
summarise(mean_expenditure = mean(expenditure_millions)) %>%
ggplot(aes(x = age,
y = mean_expenditure)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, age) %>%
summarise(total_expenditure = sum(expenditure_millions)) %>%
ggplot(aes(x = age,
y = total_expenditure)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, age) %>%
summarise(mean_visits = mean(visits_thousands)) %>%
ggplot(aes(x = age,
y = mean_visits)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, age) %>%
summarise(total_visits = sum(visits_thousands)) %>%
ggplot(aes(x = age,
y = total_visits)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, purpose) %>%
summarise(mean_expenditure = mean(expenditure_millions)) %>%
ggplot(aes(x = purpose,
y = mean_expenditure)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, purpose) %>%
summarise(totql_expenditure = sum(expenditure_millions)) %>%
ggplot(aes(x = purpose,
y = totql_expenditure)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, purpose) %>%
summarise(mean_visits = mean(visits_thousands)) %>%
ggplot(aes(x = purpose,
y = mean_visits)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, purpose) %>%
summarise(total_visits = sum(visits_thousands)) %>%
ggplot(aes(x = purpose,
y = total_visits)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, duration) %>%
count(duration) %>%
ggplot(aes(x = duration,
y = n)) +
geom_col() +
facet_wrap(~ cluster_group)

augmented %>%
filter(k == 3) %>%
group_by(cluster_group, duration) %>%
summarise(totql_expenditure = sum(expenditure_millions)) %>%
ggplot(aes(x = duration,
y = totql_expenditure)) +
geom_col() +
facet_wrap(~ cluster_group)
`summarise()` has grouped output by 'cluster_group'. You can override using the `.groups` argument.

augmented <- augmented %>%
select(-c(k, kclust, tidied, glanced))
augmented
write_csv(augmented, "clean_data/usa.csv")
LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKCmBgYHtyfQpsaWJyYXJ5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